package com.whp.yky.yky.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whp.yky.yky.config.OperationLogAnnotation;
import com.whp.yky.yky.entity.poentity.Document;
import com.whp.yky.yky.entity.Process;
import com.whp.yky.yky.response.ResponseData;
import com.whp.yky.yky.service.impl.ProcessServiceImpl;
import com.whp.yky.yky.service.serviceimpl.DocumentServiceImpl;
import com.whp.yky.yky.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "档案接口")
@CrossOrigin
@RestController
public class DocumentController {

    @Autowired
    private DocumentServiceImpl documentServiceImpl;

    @Autowired
    private ProcessServiceImpl processService;



    @ApiOperation(value="上传档案", notes="上传档案")
    @PostMapping("/insertDocument")
    public ResponseData insertDocumentInfo(@RequestBody Document document,@RequestParam String token){


        Process process =new Process();
        ResponseData res = new ResponseData();
        DecodedJWT verify = JwtUtil.verify(token);
        process.setOperatorId(Integer.valueOf(verify.getClaim("id").asString()));
        process.setDocumentId(document.getId());
        process.setStatus(0);
        processService.save(process);
        boolean result= documentServiceImpl.insertDocumentInfo(document);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else {
            res.setCode(50000);
            res.setData("fail");
        }

        return res;
    }

    @DeleteMapping("/deletDocumentByID/{id}")
    @ApiOperation(value="删除档案", notes="根据id删除档案")
    public ResponseData deletDocumentByID(@PathVariable("id") int id){
        ResponseData res = new ResponseData();
        boolean result = documentServiceImpl.fakeDeleteDocumentInfo(id);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

    @DeleteMapping("/reduceDocumentByID/{id}")
    @ApiOperation(value="删除档案", notes="根据id删除档案")
    public ResponseData reduceDocumentByID(@PathVariable("id") int id){
        ResponseData res = new ResponseData();
        boolean result = documentServiceImpl.reduceDocumentInfo(id);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

    @DeleteMapping("/deepDeletDocumentByID/{id}")
    @ApiOperation(value="删除档案", notes="根据id删除档案")
    public ResponseData deepDeletDocumentByID(@PathVariable("id") int id){
        ResponseData res = new ResponseData();
        boolean result = documentServiceImpl.deleteDocumentInfo(id);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }


    @OperationLogAnnotation(operModul = "档案模块",operType = "修改档案信息",operDesc = "修改档案信息",operUname = "修改档案信息")
    @ApiOperation(value="修改档案信息", notes="修改档案信息")
    @PostMapping("/updateDocumentInfo")
    public ResponseData updateDocumentInfo(@RequestBody Document document,@RequestParam String token){


        boolean result = false;
        ResponseData res = new ResponseData();
        Process process =new Process();
        DecodedJWT verify = JwtUtil.verify(token);
        process.setOperatorId(Integer.valueOf(verify.getClaim("id").asString()));
        process.setStatus(0);

        String download = document.getPath().substring(12);
        ;
         document.setDownload(download.substring(0,download.indexOf("\"")));

        if(document.getId()==0){
            result= documentServiceImpl.insertDocumentInfo(document);
            process.setDocumentId(document.getId());
            processService.save(process);
        }else{
            result= documentServiceImpl.updateDocumentInfo(document);
        }

        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;
    }

    @ApiOperation(value="获取所有档案信息", notes="获取所有档案信息")
    @PostMapping("/getDocumentInfo")
    public ResponseData getDocumentInfo(@RequestBody Document document, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        IPage<Document> pageList = this.documentServiceImpl.getDocumentInfo(new Page( (long)pageNum,(long)pageSize),document);
        res.setData(pageList);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

    @ApiOperation(value="获取所有档案信息", notes="获取所有档案信息")
    @PostMapping("/getReduceDocumentInfo")
    public ResponseData getReduceDocumentInfo(@RequestBody Document document, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        IPage<Document> pageList = this.documentServiceImpl.getReduceDocumentInfo(new Page( (long)pageNum,(long)pageSize),document);
        res.setData(pageList);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

    @ApiOperation(value="根据id获取档案信息", notes="根据id获取档案信息")
    @GetMapping("/getDocumentInfoById")
    public ResponseData getDocumentInfoById(@RequestParam Integer id){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        Document document = documentServiceImpl.getDocumentInfoById(id);
        res.setData(document);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

}
